package com.ssyc.gsk_teacher.activity;

import android.support.v4.app.Fragment;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ssyc.common.appupdate.AppUpdateUtil;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.fragment.TeacherHomeFragment;
import com.ssyc.gsk_teacher.fragment.TeacherMeFragment;
import com.ssyc.gsk_teacher.fragment.TeacherQuanZiFragment;
import com.ssyc.gsk_teacher.view.BottomBar;

/**
 * 主界面
 * Created by Administrator on 2018/6/22 0022.
 */
@Route(path = "/teacher/TeacherMainActivity")
public class TeacherMainActivity extends BaseActivity {

    private FrameLayout flContain;
    private BottomBar bottombar;

    //底部导航栏的文字集合
    private String[] tabTexts = {"", "", ""};

    //选中的图标集合
    private int[] selectImgs = {R.drawable.base_home_select, R.drawable.base_quanzi_select,  R.drawable.base_me_select};//R.drawable.base_message_select,

    //未选中的图标集合
    private int[] normalImgs = {R.drawable.base_home_normal, R.drawable.base_quanzi_normal,  R.drawable.base_me_normal};//R.drawable.base_message_normal,

    //当前需要显示的Framgent
    private Fragment mShowFragment;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_main;
    }

    @Override
    protected void init() {

        showContent();

        //检查更新
        AppUpdateUtil.updateVersion(this);

        //沉浸式状态栏
        StateBarManager.setBarWithUltimateBar(this, false);

        //状态栏黑色字体
//        setStatusBarDarkMode();

        //初始化视图
        initView();

        //设置初始状态
        initState();

        //初次进入显示主页的Fragment
        initHomeFramgent();

        //注册云信状态监听器
//        IMUtils.registerObservers(true);
    }


    /**
     * 初始化视图方法
     */
    private void initView() {
        flContain = findViewById(R.id.fl_contain);
        bottombar = findViewById(R.id.bottombar);
    }


    /**
     * 初始化状态的方法
     */
    private void initState() {
        bottombar.setData(selectImgs, normalImgs, tabTexts);
        //设置选中监听事件
        bottombar.setOnSelectedListener(new BottomBar.onTabSelectedListener() {
            @Override
            public void onSelected(int pos) {
                if (pos == 0) {
                    switchPage(TeacherHomeFragment.TAG, TeacherHomeFragment.class);
                } else if (pos == 1) {
                    switchPage(TeacherQuanZiFragment.TAG, TeacherQuanZiFragment.class);
                } else if (pos == 2) {
                    switchPage(TeacherMeFragment.TAG, TeacherMeFragment.class);
//                    switchPage(TeacherMessageFragment.TAG, TeacherMessageFragment.class);
                } else if (pos == 3) {
//                    switchPage(TeacherMeFragment.TAG, TeacherMeFragment.class);
                }
            }
        });
    }

    /**
     * 初次进入的时候显示主页Framgent
     */
    private void initHomeFramgent() {

        //初次进入,将首页的Fragment显示出来

        mShowFragment = TeacherHomeFragment.newInstance();

        switchPage(TeacherHomeFragment.TAG, TeacherHomeFragment.class);

    }

    /**
     * @param tag 标记名
     * @param cls 需要创建的Fragment的类名
     */
    private void switchPage(String tag, Class cls) {
        // 通过此方法我们可以查找在当前容器中是否存在该tag名的Fragment
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            try {
                // 通过反射创建出该类对象
                Fragment instance = (Fragment) cls.newInstance();
                getSupportFragmentManager().beginTransaction()
                        .hide(mShowFragment)
                        .add(R.id.fl_contain, instance, tag)
                        .commit();
                mShowFragment = instance;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            getSupportFragmentManager().beginTransaction().hide(mShowFragment) // 设置Fragment的切换动画
                    .show(fragment).commit();
            mShowFragment = fragment;
        }
    }
}
